Lesson
5: Triggers in JASS
Overview
Now, if all you've ever
known is GUI, you're used to everything being a trigger. GUI is just
a system of triggers, but you're probably wondering what exactly is a
trigger. This lesson will cover triggers in JASS and how to use them.
First, I'd like to tell you exactly what a trigger is.
What
is a Trigger?
I'm sure that you know
what a trigger is if you've used GUI before, but GUI hides some
important things from you. A trigger is really just another object,
like a unit or a special effect. What makes triggers useful is that
they are able to detect events. Without triggers, you couldn't do too
much in the editor without a lot of trouble. Now, here's an example
of making a trigger in JASS:
Now, this may be a bit
overwhelming at first, but I'll explain everything well enough so
that you should understand. First off, you're going to create a local
variable for the trigger. This is fine if you don't need other
triggers to run TriggerExecute on this trigger (TriggerExecute is a
function that runs a trigger). Now, whenever you create a new
trigger, you must use the function CreateTrigger(). It's like using
the CreateUnit function to create a unit. Now that you have created a
trigger, you'll need to add some things to it so it would be useful.
The next line adds an event to the trigger.
TriggerRegisterPlayerEvent takes 3 values, which are the trigger you
want to add the event to, the player this event applies to, and the
specific event. There are many other TriggerRegister... functions
which you can experiment with later, but for now this will be fine.
The next line adds an action which is basically telling the trigger
to call that function when the event happens. Now, I didn't show you
the function MyFunction, but you need to know that it must take
nothing and return nothing. Now, the final line sets the local
variable to null, which is necessary for local variables which are
handles so that memory leaks can be prevented. If you don't know what
a memory leak is, don't worry about it, they will be covered in a
later tutorial.
Okay, now you're probably still using that GUI
trigger to run your test functions in the custom script. This JASS
trigger does pretty much the exact same thing as the GUI trigger, so
you can replace it. Just create a new trigger, and then go to Edit ->
Convert to Custom Text. What you will see is something similar to the
function shown above, only it's setting a global variable
automatically generated by the editor to a trigger and just adding an
action. Delete all the code and make a new function that has the
name: InitTrig_[insert trigger name here]. So if the name of your
trigger was JASSTrigger, you would have your function be: function
InitTrig_JASSTrigger takes nothing returns nothing It's important
that you name it this way so that it gets called while the map is
loading.
Now, if you've noticed, I've left out how to add a
condition, oh my! Well, I'll show you how adding conditions work in
triggers. Don't convert a GUI trigger to JASS to see how to do
conditions, that way is inefficient. There's more code than you
really need to use. Let me show you an example:
This might look confusing, it might
not. Either way I'm going to review what everything does. First off,
I'll review how to set up a function so it can be used as a condition
in a trigger. First off, you can name the function whatever you want,
but it has to take nothing and return boolean. A boolean value, as
you should know, is true/false. This function returns
GetTriggerPlayer() == Player(0). You might look at that and be
clueless, others might understand it already. The first part gets the
player who triggered the event (the player pressing escape). And
finally, the == Player(0) part checks if the player is Player(0)
(Player 1 (Red)). Since this is a boolean statement, meaning it is
either true or false, it can be used as a return value.
Finally,
we have the added line in the InitTrig_JASSTrigger function. The
function TriggerAddCondition takes a trigger and a boolexpr (boolean
expression) . You don't need to know what a boolexpr is yet, all you
need to know is that you put Condition(function [insert trigger name
here]) and it returns a boolexpr which is used for conditions in
triggers.
Challenge
This challenge will require a bit
of researching JASS functions, so I recommend you get a tool such as
JassCraft. Well, I'll give you three possible challenges. The first
challenge will to be to create a trigger in JASS that will run a
function that you create (any). After you've messed with that a bit,
open up JassCraft or a similar program where you can search JASS
functions. You're next challenge will be to create a trigger that
will go off whenever you type in "-run" or whatever kind of
text message in-game. I'll give you some help in telling you that the
function you're looking for to add the event is
TriggerRegisterPlayerChatEvent, however it's up to you to figure out
how to use it. If you've solved that challenge, then make a trigger
that goes off when the message a player types contains the character
"-" or whatever letter you choose. In the actions, you will
then display the value the message the player typed in. Hints for
this challenge are keep using the same function to add the event,
only you need to change a value or two. And I suppose it would also
be helpful to tell you that the GetEventPlayerChatString can be used
to get the string that the player types in (used in the function
called when the trigger goes off).
Good Luck -wyrmlord